

package myclass;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import static java.awt.image.ImageObserver.WIDTH;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.BoundedRangeModel;
import javax.swing.Box;
import javax.swing.DefaultBoundedRangeModel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ChangeListener;


public class OutputTable extends JPanel
{
    private JTable resultTable;
    private BackgroundWorker utils;
    private JProgressBar progressBar;
    private JButton cancelButton;
    private int currentProgress;
    private JButton startButton;
    
    
    public OutputTable(){
    
        resultTable = new JTable();
        utils = new BackgroundWorker();
        cancelButton = new JButton();
        startButton = new JButton();
        progressBar = new JProgressBar(new DefaultBoundedRangeModel(0, 0, 0, 100));
        
        init();
    }

    public JTable getResultTable()
    {
        return resultTable;
    }
    
    
    
    private void init(){
    
        cancelButton.setAction(new AbstractAction("Cancel")
        {

            @Override
            public void actionPerformed(ActionEvent ae)
            {
                if(!utils.isDone())
                {
                utils.cancel(true);
                }
            }
        });
        
        startButton.setAction(new AbstractAction("Start")
        {

            @Override
            public void actionPerformed(ActionEvent ae)
            {
                resultTable.getModel().setValueAt(ae, WIDTH, WIDTH);
                while(utils.getProgress() != 100){
                    progressBar.setValue(utils.getProgress());
                }
            }
        });
        
        JScrollPane jScrollPane = new JScrollPane(resultTable);
        setLayout(new BorderLayout());
        
        add(jScrollPane, BorderLayout.CENTER);

        Box buttonBox = Box.createHorizontalBox();
        buttonBox.add(startButton);
        buttonBox.add(Box.createHorizontalGlue());
        buttonBox.add(cancelButton);

        add(buttonBox, BorderLayout.SOUTH);
        
    }
}
